﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Specialized;
using MM.LibraryAPI.YL;
using MM.Common;

namespace WebAPI.YL
{
    public partial class Notify : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                //LogHelper.WriteLog("验证前");

                Dictionary<String, String> para = new Dictionary<String, String>();
                NameValueCollection coll;
                coll = Request.Form;
                if (coll == null)
                {
                    Response.Write("fail");
                    return;
                }
                String[] requestItem = coll.AllKeys;
                for (int i = 0; i < requestItem.Length; i++)
                {
                    para.Add(requestItem[i], Request.Form[requestItem[i]]);
                }

                if (para.Count > 0)
                {
                    //LogHelper.WriteLog("参数个数" + para.Count);
                    if (UpmpService.VerifySignature(para))
                    {   //服务器签名验证成功
                        //请在这里加上商户的业务逻辑程序代码
                        //获取通知返回参数，可参考接口文档中通知参数列表(以下仅供参考)
                        String transStatus = Request.Form["transStatus"];// 交易状态

                        //LogHelper.WriteLog("交易状态" + transStatus);
                        if (!string.IsNullOrEmpty(transStatus) && "00" == transStatus)
                        {
                            // 交易处理成功
                            string OrderID = Request.Form["orderNumber"];
                            if (!string.IsNullOrEmpty(OrderID))
                            {
                                MM.LibraryAPI.Common.SetOrderStatus(OrderID);
                            }

                            Response.Write("success");
                        }
                    }
                    else//服务器签名验证失败
                    {
                        Response.Write("fail");
                    }
                }
                else
                {
                    Response.Write("通知参数为空");
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("银联返回信息异常WebAPI.YL：", ex);
            }
        }
    }
}